package com.heei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @program: spring-cloud-learning
 * @description: 消费者 接口
 * @author: HuPing
 * @create: 2020-10-22 11:45
 **/
@RestController
public class DcController {

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/consumer")
    public String consumer() {

        ServiceInstance serviceInstance = loadBalancerClient.choose("consul-client");
        String clientDcUrl = "http://" + serviceInstance.getHost()+":"+serviceInstance.getPort()+"/dc";

        return new RestTemplate().getForObject(clientDcUrl, String.class);
    }

    @GetMapping("/consumerHealth")
    public String consumerHealth() {

        ServiceInstance serviceInstance = loadBalancerClient.choose("consul-client");
        String clientDcUrl = "http://" + serviceInstance.getHost()+":"+serviceInstance.getPort()+"/health";

        return new RestTemplate().getForObject(clientDcUrl, String.class);
    }
}
